package cn.itcast.service.user.impl;

import cn.itcast.dao.user.UserDao;
import cn.itcast.domain.User;
import cn.itcast.service.user.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public PageInfo findAll(int pageNum, int pageSize, String companyId) {

        PageHelper.startPage(pageNum,pageSize);
        List<User> list = userDao.findAll(companyId); //分页查
        PageInfo pb = new PageInfo(list);
        return pb;
    }
    
    @Override
    public void save(User user) {
        
        userDao.save(user);
    }

    @Override
    public User findById(String id) {
        
        return userDao.findById(id);
    }

    @Override
    public void update(User user) {
        
        userDao.update(user);
    }

    @Override
    public void delete(String id) {
        
        userDao.delete(id);
    }

    @Override
    public void changeRole(String userid, String[] roleIds) {
        // 删除该用户以前的角色
        userDao.deleteUserRole(userid);
        // 新增该用户新的角色
        for (String roleId : roleIds) {
            userDao.saveUserRole(userid,roleId);
        }
    }

    @Override
    public User findByUser(String email) {
        return userDao.findByUser(email);
    }
}
